
#include "catch2/catch.hpp"
#include <iostream>
#include <thread>
#include <vector>
#include "rwlock.hpp"

RWLock rwlock;

void reader(int id)
{
    rwlock.rlock();
    std::cout << "Reader " << id << " is reading." << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    rwlock.unlock();
}

void writer(int id)
{
    rwlock.wlock();
    std::cout << "Writer " << id << " is writing." << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    rwlock.unlock();
}

TEST_CASE("test_rwlock", "[rwlock]")
{
    std::vector<std::thread> threads;

    // Create multiple readers and writers
    for (int i = 0; i < 5; ++i)
    {
        threads.emplace_back(reader, i);
    }

    threads.emplace_back(writer, 1);

    for (auto &t : threads)
    {
        t.join();
    }
};